#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""
A static set of resource caches for
caching environmentally-bound resources.
"""

namespace Grawlix.Services.Resources
import Grawlix.Services
import System
import System.Runtime.Serialization
import System.Security.Permissions

final class ResourceRepository[of
	T/*(interface, IDynamicResource)*/](
	PermanentMarshalByRefObject,
	ISerializable):
"""Type-based caching for environment-based resources."""
	
	internal def constructor(
		[required] factory as AbstractResourceFactory[of T]):
	"""
	Initializes the repository.
	Remarks: Only one repository per resource type will
		be able to be used per AppDomain.
	"""
		_factory = factory
	
	[SecurityPermission(
		SecurityAction.LinkDemand,
		Flags: SecurityPermissionFlag.SerializationFormatter)]
	def ISerializable.GetObjectData(
		[required] info as SerializationInfo,
	    context as StreamingContext):
	"""
	Get serialization info that can be used across AppDomains.
	Param info: The Serialization info available.
	Param context: The StreamingContext of the serialization.
	"""
		info.SetType(NullObjectReference)
	
	def RequestUpdate(
		[required] appDomain as AppDomain,
		[required] destination as ResourceCache[of T],
		[required] identifier as Uri):
	"""
	Requests that the most recent data be sent to the destination.
	Param appDomain: The referencing AppDomain.
	Param destination: The cache from the referencing AppDomain.
	Param identifier: The URI of the resource.
	Returns: The marshalled resource.
	"""
		_factory.RequestUpdate(appDomain, destination, identifier)
	
	def RequestRemoval(
		[required] domain as AppDomain,
		[required] identifier as Uri):
	"""
	Requests removal of a reference to a resource.
	Param domain: The referencing domain.
	Param identifier: The URI of the resource.
	"""
		_factory.RequestRemoval(domain, identifier)
	
	override def ToString():
	"""
	Get a string representation for this object.
	Returns: A string representation for this object.
	"""
		return "ResourceRepository<${typeof(T).Name}>"
	
	Creator:
	"""A MarshalByValue object that can create proxies."""
		get:
			return _factory.Creator
	
	private _factory as AbstractResourceFactory[of T]
	"""The internal resource factory."""
	
